﻿
package com.android.finaltracer.usermanage;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.android.finaltracer.engine.EventNotify;

public class PhoneManager {
    // 指的是 本手机相对于这个list里面的手机是master 即本手机作为控制手机，下属的手机list
    private ArrayList<GpsTracerPhone> masterPhoneList;
    private ArrayList<GpsTracerPhone> slavePhoneList;
    private ArrayList<BindPhone> bindPhoneList;
    private static PhoneManager instance;
    private final static String TAG = "PhoneManager";

    private PhoneManager() {
        super();
        masterPhoneList = new ArrayList<GpsTracerPhone>();
        slavePhoneList = new ArrayList<GpsTracerPhone>();
        bindPhoneList = new ArrayList<BindPhone>();
    }

    public static PhoneManager getInstance() {
        if (instance == null) {
            instance = new PhoneManager();
            Log.e("wanghp", "new phone manager : " + instance);
        }
        return instance;
    }

    public GpsTracerPhone addMasterPhone(String phoneNum, Context mContext,
            EventNotify mEventNotify) {
        Log.i(TAG, "addMasterPhone phoneNum = " + phoneNum);
        if (phoneNum == null || phoneNum.length() == 0) {
            return null;
        }
        if (masterPhoneList == null) {
            masterPhoneList = new ArrayList<GpsTracerPhone>();
        }
        GpsTracerPhone toAddPhone = null;
        for (GpsTracerPhone phone : masterPhoneList) {
            if (phone.isSamePhone(phoneNum)) {
                toAddPhone = phone;
                phone.reInit();
                break;
            }
        }
        if (toAddPhone == null) {
            toAddPhone = new GpsTracerPhone(phoneNum, mContext, mEventNotify);
            masterPhoneList.add(toAddPhone);
        }
        return toAddPhone;
    }

    public GpsTracerPhone addSlavePhone(String phoneNum, Context mContext,
            EventNotify mEventNotify) {
        if (phoneNum == null || phoneNum.length() == 0) {
            return null;
        }
        if (slavePhoneList == null) {
            slavePhoneList = new ArrayList<GpsTracerPhone>();
        }
        GpsTracerPhone toAddPhone = null;
        for (GpsTracerPhone phone : slavePhoneList) {
            if (phone.isSamePhone(phoneNum)) {
                toAddPhone = phone;
                phone.reInit();
                break;
            }
        }
        if (toAddPhone == null) {
            toAddPhone = new GpsTracerPhone(phoneNum, mContext, mEventNotify);
            slavePhoneList.add(toAddPhone);
        }
        return toAddPhone;
    }

    public BindPhone addBindPhone(String phoneNum, Context mContext,
            EventNotify mEventNotify) {
        if (phoneNum == null || phoneNum.length() == 0) {
            return null;
        }
        if (bindPhoneList == null) {
            bindPhoneList = new ArrayList<BindPhone>();
        }
        BindPhone toAddPhone = null;
        for (BindPhone phone : bindPhoneList) {
            if (phone.isSamePhone(phoneNum)) {
                toAddPhone = phone;
                phone.reInit();
                break;
            }
        }
        if (toAddPhone == null) {
            toAddPhone = new BindPhone(phoneNum, mContext, mEventNotify);
            bindPhoneList.add(toAddPhone);
        }
        return toAddPhone;
    }

    public boolean removeMasterPhone(String phoneNum) {
        Log.i(TAG, "removeMasterPhone phoneNum = " + phoneNum);

        boolean success = false;
        if (masterPhoneList == null || phoneNum == null
                || phoneNum.length() == 0) {
            return false;
        }
        for (int i = 0; i < masterPhoneList.size(); i++) {
            if (masterPhoneList.get(i).isSamePhone(phoneNum)) {
                masterPhoneList.remove(i);
                success = true;
                break;
            }
        }
        return success;
    }

    public boolean removeSlavePhone(String phoneNum) {
        boolean success = false;
        if (slavePhoneList == null || phoneNum == null
                || phoneNum.length() == 0) {
            return false;
        }
        for (int i = 0; i < slavePhoneList.size(); i++) {
            if (slavePhoneList.get(i).isSamePhone(phoneNum)) {
                slavePhoneList.remove(i);
                success = true;
                break;
            }
        }
        return success;
    }

    public boolean removeBindPhone(String phoneNum) {
        boolean success = false;
        if (bindPhoneList == null || phoneNum == null || phoneNum.length() == 0) {
            return false;
        }
        for (int i = 0; i < bindPhoneList.size(); i++) {
            if (bindPhoneList.get(i).isSamePhone(phoneNum)) {
                bindPhoneList.remove(i);
                success = true;
                break;
            }
        }
        return success;
    }

    public ArrayList<GpsTracerPhone> getMasterPhoneList() {
        return masterPhoneList;
    }

    public ArrayList<GpsTracerPhone> getSlavePhoneList() {
        return slavePhoneList;
    }

    public ArrayList<BindPhone> getBindPhoneList() {
        return bindPhoneList;
    }

    public GpsTracerPhone getSpecifyMasterPhone(String PhoneNumber) {
        GpsTracerPhone retPhone = null;
        for (GpsTracerPhone phone : masterPhoneList) {
            if (phone.isSamePhone(PhoneNumber)) {
                retPhone = phone;
                break;
            }
        }
        return retPhone;
    }

    public GpsTracerPhone getSpecifySlavePhone(String PhoneNumber) {
        GpsTracerPhone retPhone = null;
        for (GpsTracerPhone phone : slavePhoneList) {
            if (phone.isSamePhone(PhoneNumber)) {
                retPhone = phone;
                break;
            }
        }
        return retPhone;
    }

    public BindPhone getSpecifyBindPhone(String PhoneNumber) {
        BindPhone retPhone = null;
        for (BindPhone phone : bindPhoneList) {
            if (phone.isSamePhone(PhoneNumber)) {
                retPhone = phone;
                break;
            }
        }
        return retPhone;
    }

    public void onClear() {

        Log.i(TAG, "phoneManager onClear = ");

        if (masterPhoneList != null) {
            masterPhoneList.clear();
        }
        if (slavePhoneList != null) {
            slavePhoneList.clear();
        }
        if (bindPhoneList != null) {
            bindPhoneList.clear();
        }
    }
}
